<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('session');
		$this->load->library('pagination');
		$this->load->helper(array('form', 'url'));
		$this->load->helper("text");

	}
	public function index()
	{
		$this->load->model('category_model');
		$this->load->model('question_model');
		$data['list_category'] = $this->category_model->listallcategory();
		$data['news_question'] = $this->question_model->list_all_question_active();
		$this->load->view('frontend/index',$data);
		if(isset($_REQUEST['"submitbutton"'])){
			redirect('game/add');
		}
	}

	public function cate(){
		$this->load->model('category_model');
		$this->load->model('question_model');
		$data['list_category'] = $this->category_model->listallcategory();
		$data['news_question'] = $this->question_model->list_all_question_active();
		$this->load->view('frontend/index',$data);
		if(isset($_REQUEST['"submitbutton"'])){
			redirect('game/add');
		}
	}

	public function ajax_show_question(){
		if(isset($_REQUEST['makequestion'])){
			$fullname = $this->input->post('fullname', true);
			$youremail = $this->input->post('youremail', true);
			$title_question = $this->input->post('title_question', true);
			$content_question = $this->input->post('content_question', true);
			$this->load->model('question_model');
			$this->question_model->insert_question(1,$fullname,$youremail,$title_question,$content_question);
			redirect('game/add_question/'.md5($fullname));
		}
		$this->load->view('frontend/ajax_make_question');
	}

	public function add($result=null){

		$this->load->model('category_model');
		$this->load->model('question_model');
		$data['list_category'] = $this->category_model->listallcategory();
		$data['news_question'] = $this->question_model->list_all_question_active();
			
		if(isset($_REQUEST['makequestion'])){
			$fullname = $this->input->post('fullname', true);
			$youremail = $this->input->post('youremail', true);
			$title_question = $this->input->post('title_question', true);
			$content_question = $this->input->post('content_question', true);
			$this->load->model('question_model');
			$this->question_model->insert_question(1,$fullname,$youremail,$title_question,$content_question);
			$newdata = array(
						'guicauhoi'  => $fullname 
			);
			$this->session->set_userdata($newdata);
			redirect('game/add/'.md5('successful'));
		}
		if($result=="new"){
			$data['alert_mess'] = null;
		}elseif($result==md5('successful')){
			$data['alert_mess'] = "Cám ơn bạn <b>" .$this->session->userdata('guicauhoi'). "</b> đã gửi câu hỏi";
			$this->session->unset_userdata('guicauhoi');
		}else{
			redirect('game');
		}

		$this->load->view('frontend/index',$data);

	}

	public function category($cate_id=null) {
		$this->load->model('category_model');
		$this->load->model('question_model');
		$this->load->model('answer_model');
		$data['list_category'] = $this->category_model->listallcategory();
		$data['questions'] = $this->question_model->get_question_by_cate($cate_id);
		foreach ($data['questions'] as $question) {
			$answer = $this->answer_model->get_answer_by_questionid($question->id);
			if (count($answer) == 0) {
			}
			$data['answers'][] = $answer[0];
		}
		$this->load->view('frontend/index',$data);
		//echo $cate_id;
	}
	
	public function ans($title=null){
		$id_question = $this->returnquestionid($title);
		$this->load->model('answer_model');
		$this->load->model('question_model');
		$this->load->model('category_model');
		if(isset($_REQUEST['makequestion'])){
			$fullname = $this->input->post('fullname', true);
			$email = $this->input->post('youremail',true);
			$content = $this->input->post('content_question',true);
			$this->answer_model->add_answer($id_question,$content,$fullname,$email);
			redirect('game/details/'.$title);
		}
		
	}

	public function scam($id=null){
		$this->load->model('answer_model');
		$this->answer_model->update_scam($id);
		redirect('game');
	}
	public function details($title=null){
		$id_question = $this->returnquestionid($title);
		$this->load->model('answer_model');
		$this->load->model('question_model');
		$this->load->model('category_model');
		$data['questions'] = $this->question_model->questiondetail($id_question);
		$data['list_answer'] = $this->answer_model->get_answer_by_questionid($id_question);
		$data['list_category'] = $this->category_model->listallcategory();
		$this->load->view('frontend/index',$data);
	}

	public function returnquestionid($title = null){
		$this->load->model('question_model');
		$id = $this->question_model->get_details_question($this->removesign($title));
		return $id;
	}


	//xu ly chuyen tieng viet co dau thanh khong dau
	function removesign($str)
	{
		$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
		,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à");
				$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a");
				return str_replace($coDau,$khongDau,$str);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */